// 按需导入router实例 hash
import { createRouter, createWebHashHistory } from 'vue-router';

// 导入界面
import Notes from './components/notes.vue'
import Login from "./components/personal Center/login.vue";
import Reguser from "./components/personal Center/reguser.vue";

// 常见router实例
const router = createRouter({
    // 切换的模式
    history: createWebHashHistory(),
    // 通过routes 数组，指定路由规则
    routes: [
        // path 是hash地址，component 是要展示的组件
        // 重定向
        { path: '/', redirect: '/login' },
        { path: '/notes', component: Notes },
        { path: '/login', component: Login },
        { path: '/reguser', component: Reguser }
    ]
})

import axios from 'axios'

router.beforeEach(async (to, from, next) => {
    if (to.path === '/login' || to.path === '/register') return next()
    if (to.path === '/notes') {
        const tokenStr = localStorage.getItem('token')
        if (!tokenStr) return next('/login')

    }
    next()
})

// 向外暴露router
export default router